<?php

namespace Apps\Common\Models;


/**
 * 问题Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class Question extends \PhalconPlus\Database\Model
{
    /**
     * @var integer
     */
    public $question_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var integer
     */
    public $question_tag;

    /**
     *
     * @var string
     */
    public $question_content;

    /**
     *
     * @var string
     */
    public $question_pics;

    /**
     * @var integer
     */
    public $answer_num;

    /**
     *
     * @var integer
     */
    public $question_browsers;

    /**
     *
     * @var integer
     */
    public $question_state;

    /**
     *
     * @var string
     */
    public $question_addtime;
    public $is_expert_reply;
    public $question_follows;
    public $last_reply_time;
    public $city_code;
	public $is_manager_reply;
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('question_id', 'Apps\Common\Models\QuestionAnswer', 'question_id', array('alias' => 'QuestionAnswer'));
        $this->hasMany('question_id', 'Apps\Common\Models\QuestionFollow', 'question_id', array('alias' => 'QuestionFollow'));
        $this->belongsTo('user_id', 'Apps\Common\Models\UserBase', 'user_id', array('alias' => 'UserBase'));
        $this->belongsTo('question_tag', 'Apps\Common\Models\QuestionTag', 'tag_id', array('alias' => 'QuestionTag'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'question';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Question[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Question
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'question_id' => 'question_id',
            'user_id' => 'user_id',
            'question_tag' => 'question_tag',
            'question_content' => 'question_content',
            'question_pics' => 'question_pics',
            'answer_num' => 'answer_num',
            'question_browsers' => 'question_browsers',
            'question_state' => 'question_state',
            'is_expert_reply' => 'is_expert_reply',
            'question_addtime' => 'question_addtime',
            'question_follows' => 'question_follows',
            'last_reply_time' => 'last_reply_time',
            'city_code' => 'city_code',
        	'is_manager_reply'=>'is_manager_reply'
        );
    }

    public function defaultColumns()
    {
        return [
            'question_pics',
            'answer_num',
            'question_browsers',
            'question_state',
            'question_addtime',
        ];
    }

    /**
     * 更新回答数
     * @param $id
     * @return bool
     */
    public static function updateAnswerNum($id)
    {
        $sql = 'UPDATE question SET answer_num = (SELECT COUNT(*) FROM question_answer WHERE question_id=question.question_id AND answer_state > 0) WHERE question_id = :qid';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['qid' => $id]);
    }

    /**
     * 增加/减少评论数
     * @param int $question_id 问答id
     * @param int $count 增加/减少评论的数量
     * @param bool $type true~增加，false~减少
     */
    public function setAnswerNum($question_id, $count = 1, $type = true)
    {
        if ($type) {
            $dateTime = date('Y-m-d H:i:s');
            (new self)->getReadConnection()->query("UPDATE question SET last_reply_time = '{$dateTime}',answer_num = answer_num + {$count} WHERE question_id = {$question_id}");
        } else {
            (new self)->getReadConnection()->query("UPDATE question SET answer_num = answer_num - {$count} WHERE question_id = {$question_id}");
        }
    }

    /**
     * 更新关注数
     * @param $id
     * @return bool
     */
    /*public static function updateFollows($id)
    {
        $sql = 'UPDATE question SET question_follows = (SELECT COUNT(*) FROM question_follow WHERE question_id=question.question_id) WHERE question_id = :qid';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['qid' => $id]);
    }*/
}
